<?php

namespace App\Imports;


use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use stdClass;
use App\Models\DeptModel;
use App\Models\AdminRoleModel;
use PhpOffice\PhpSpreadsheet\Shared\Date;

class StaffImport implements ToCollection, WithMultipleSheets
{


    private $result;

    public function __construct() {
        $this->result = new stdClass;
    }

    public function collection(Collection $rows){
        $lines = [];
        $department_arr = [];
        $role_arr = [];

//        $data = [];
//        foreach ($rows as $k => $row){
//            if($k == 0) continue;
//            foreach ($row as $k2 => $v){
//                $data[$rows[0][$k2]] = $v;
//            }
//        }

        foreach ($rows as $k => $row) {
            if($k == 0) continue;

            $department = $row[1] ?? '';
            $role = $row[2] ?? '';
            $dept_id = 0;
            $role_id = 0;

            if(!empty($department)) {
                if(isset($department_arr[$department])){
                    $dept_id = $department_arr[$department];
                }else{
                    $find = DeptModel::query()->where('name', $department)->first();
                    if(!$find) {
                        $this->result->error[] = '部门【'.$department.'】不存在';
                    }else{
                        $dept_id = $find->id;
                        $department_arr[$department] = $dept_id;
                    }
                }
            }

            if(!empty($role)) {
                if(isset($role_arr[$role])){
                    $role_id = $role_arr[$role];
                }else{
                    $find = AdminRoleModel::query()->where('name', $role)->first();
                    if(!$find) {
                        $this->result->error[] = '角色【'.$role.'】不存在';
                    }else{
                        $role_id = $find->id;
                        $role_arr[$role] = $role_id;
                    }
                }
            }

            if(is_int($row[6])){
                $employment_start_date = $row[6] ? Date::excelToDateTimeObject($row[6])->format('Y-m-d') : NULL;
            }else{
                $employment_start_date = $row[6];
            }

            if(is_int($row[34])){
                $contract_renewal_date = $row[34] ? Date::excelToDateTimeObject($row[34])->format('Y-m-d') : NULL;
            }else{
                $contract_renewal_date = $row[34];
            }

            if(is_int($row[8])){
                $turn_date = $row[8] ? Date::excelToDateTimeObject($row[8])->format('Y-m-d') : NULL;
            }else{
                $turn_date = $row[8];
            }

            if(is_int($row[33])){
                $contract_end_date = $row[33] ? Date::excelToDateTimeObject($row[33])->format('Y-m-d') : NULL;
            }else{
                $contract_end_date = $row[33];
            }

            if(is_int($row[31])){
                $contract_last_date = $row[31] ? Date::excelToDateTimeObject($row[31])->format('Y-m-d') : NULL;
            }else{
                $contract_last_date = $row[31];
            }

            if(is_int($row[10])){
                $birth_date = $row[10] ? Date::excelToDateTimeObject($row[10])->format('Y-m-d') : NULL;
            }else{
                $birth_date = $row[10];
            }

            if(is_int($row[41])){
                $create_date = $row[41] ? Date::excelToDateTimeObject($row[41])->format('Y-m-d') : NULL;
            }else{
                $create_date = $row[41];
            }

            $lines[] = [
                'dept_id' => $dept_id,
                'role_id' => $role_id,
                'name' => $row[3],
                'name_en' => $row[4],
                'worker_number' => $row[5],
                'employment_start_date' => $employment_start_date,
                'probation_period_months' => $row[7],
                'turn_date' => $turn_date,
                'gender' => $row[9] == '男' ? 1 : 2,
                'birth_date' => $birth_date,
                'age' => $row[11],
                'phone' => $row[12] ? number_format($row[12], 0, '', '') : '',
                'email' => $row[13],
                'native_place' => $row[14],
                'id_number' => $row[15] ? number_format($row[15], 0, '', '') : '',
                'home_place' => $row[16],
                'nationality' => $row[17],
                'addr_now' => $row[18],
                'education_level' => $row[19],
                'major' => $row[20],
                'school_graduated' => $row[21],
                'political_status' => $row[22],
                'marital_status' => $row[23] == '已婚' ? 1 : 0,
                'emergency_contact_person' => $row[24],
                'emergency_contact_relation' => $row[25],
                'emergency_contact_phone' => $row[26],
                'work_years' => $row[27],
                'work_month' => $row[28],
                'year_holidays' => $row[29],
                'contract_times' => $row[30],
                'contract_last_date' => $contract_last_date,
                'contract_years' => $row[32],
                'contract_end_date' => $contract_end_date,
                'contract_renewal_date' => $contract_renewal_date,
                'contract_end_remind' => $row[35],
                'social_security_number' => $row[36] ? number_format($row[36], 0, '', '') : '',
                'housing_fund_number' => $row[37] ? number_format($row[36], 0, '', '') : '',
                'salary_card_bank' => $row[38],
                'salary_card_number' => $row[39] ? number_format($row[39], 0, '', '') : '',
                'remark' => $row[40],
                'create_date' => $create_date,
                'creator' => $row[42]
            ];
        }

        $this->result->lines = $lines;
    }

    public function sheets(): array {
        return [ 0 => $this];
    }

    public function get_result(){
        return $this->result;
    }


}
